#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTextCodec>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    m_serialPort = nullptr;
    connect(ui->comboBox, &QComboBox::currentIndexChanged, this, &MainWindow::slotOpenSerialPort);
    connect(ui->comboBox_2, &QComboBox::currentIndexChanged, this, &MainWindow::senddate);
    zhiling.append("openbeep");
    zhiling.append("closebeep");
    for(QString str:zhiling)
        ui->comboBox_2->addItem(str);
    slotLoadSerialPort();
    ui->centralwidget->setObjectName("centralWidget"); // 设置对象名称
    ui->centralwidget->setStyleSheet("QWidget#centralWidget { background-image: url(:/build/1.jpg); background-size: cover; }");
}
MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::slotCloseSerialPort()
{
    if (m_serialPort)
        m_serialPort->close();
}
void MainWindow::slotLoadSerialPort()
{
    m_serialPortName.clear();
    foreach(const QSerialPortInfo &info,QSerialPortInfo::availablePorts()) //获取有效的串口列表
    {
        m_serialPortName << info.portName();
        ui->comboBox->addItem(info.portName());
        qWarning()<<"serialPortName:"<<info.portName();
    }
}
void MainWindow::slotOpenSerialPort(int index)
{
    if(index <0 ||index >=m_serialPortName.size())
    {
        return ;
    }
    QString selectportname = m_serialPortName[index];
    m_serialPort = new QSerialPort(this);//新建一个窗口对象

    if(m_serialPort->isOpen())//判断对象是否已经打开，如果是就关掉
    {
        m_serialPort->clear();
        m_serialPort->close();
    }
    //设置串口名字 打开串口
    m_serialPort->setPortName(selectportname);
    //打开串口
    if (!m_serialPort->open(QIODevice::ReadWrite))//打开COM2串口，并且设置为读写状态
    {
        // 打开失败，可以添加错误处理代码
        qDebug()<<"打开失败";
        return;
    }
    m_serialPort->setBaudRate(115200);//波特率
    m_serialPort->setDataBits(QSerialPort::Data8);//数据位
    m_serialPort->setFlowControl(QSerialPort::NoFlowControl);//数据流控制（暂时不管）
    m_serialPort->setParity(QSerialPort::NoParity);//奇偶校验位
    m_serialPort->setStopBits(QSerialPort::OneStop);//停止位
    //把串口的可读信号和槽函数连接
    connect(m_serialPort,SIGNAL(readyRead()),this,SLOT(slotReadData()));
}
void MainWindow::slotReadData()
{
    QByteArray rawData = m_serialPort->readAll();
    QTextCodec *codec = QTextCodec::codecForName("GB2312");
    QString data = codec->toUnicode(rawData);
    qWarning()<<"接收数据："<<data;
    ui->lineEdit_2->setText(data);

}
void MainWindow::senddate(int index)
{
    sdate = zhiling[index];
}
void MainWindow::slotSend()
{
    if (m_serialPort)
    {
        if (!m_serialPort->isOpen())
            qWarning()<<"请打开一个串口";
        QString info = ui->lineEdit->text();
        //info = sdate;
        if(!sdate.isEmpty())//如果选择指令不为空则覆盖
            info = sdate;
        QByteArray sendBuf;
        //把字符串转为字节
        qDebug() << info;
        sendBuf.append(info.toLocal8Bit());// = info.toLocal8Bit();
        m_serialPort->write(sendBuf);
    }
}
void MainWindow::on_pushButton_clicked()
{
    slotSend();
}

